<?php
namespace app\a\controller;
use think\facade\Db;
class Page extends Base{
    public function get_list(){
        $param = input("");
        
        $search_type = $param['type'] ? ['type_id' => $param['type']]:[];//判断是否需要筛选 分类
        $search = $param['keyWord'] ? [['cn_name', 'like', '%'.$param['keyWord'].'%']]:[];//判断是否需要筛选 关键词
        $search_pid = isset($param['pid']) ? ['pid' => $param['pid']]:[];
        $search_sort = $param['prop'] ? [$param['prop'] => $param['sort']]:[];//排序

        $listData = Db::table('an_page')
        ->page($param['page'],$param['limit'])
        ->where($search)
        ->where($search_type)
        ->where($search_pid)
        ->order($search_sort)
        ->select()
        ->toArray(); 

        $this->info['data'] = [
            'list' => $listData,
            'type' => Db::table('an_type')->where('type', 'pages')->order('id', 'desc')->select()->toArray()
        ];

        $this->info['total'] = Db::table('an_page')
        ->where($search)
        ->where($search_type)
        ->where($search_pid)
        ->count();
        return json($this->info);
    }

    public function get_detail(){
        $param = input('');
        $details = null;
        if($param['id']){
            $details = Db::table('an_page')->where('id',$param['id'])->find();
        }
        $this->info['data'] = [
            'details'=>$details,
            'type' =>[]
        ];
        return json($this->info);
    }

    //删除数据
    public function del_data(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_page')->where('id',$v)->delete();
        }
        return json($this->info);
    }

    public function add_data(){
        $param = input('');
        Db::table('an_page')->insert([
            'create_time'=>time(),
            'up_time'=>time(),
            'type_id'=>$param['type_id']?$param['type_id']:1,
            'img'=>isset($param['img'])?$param['img']:'',
            'cn_name'=>isset($param['cn_name'])?$param['cn_name']:'',
            'en_name'=>isset($param['en_name'])?$param['en_name']:'',
            'title'=>isset($param['title'])?$param['title']:'',
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'description'=>isset($param['description'])?$param['description']:'',
            'abstract'=>isset($param['abstract'])?$param['abstract']:'',
            'content'=>isset($param['content'])?$param['content']:'',
            'url'=>isset($param['url'])?$param['url']:'',
            'jump'=>isset($param['jump'])?$param['jump']:'default',
            'other'=>isset($param['other'])?$param['other']:'',
            'sort'=>isset($param['sort'])?$param['sort']:0,
            'pid'=>isset($param['pid'])?$param['pid']:0,
            "a_show"=>$param['a_show'],
        ]);
        return json($this->info);
    } 
    
    //更新数据
    public function updata(){
        $param = input('');
        Db::table('an_page')->where('id',$param['id'])->update([
            'up_time'=>time(),
            'type_id'=>$param['type_id']?$param['type_id']:1,
            'img'=>isset($param['img'])?$param['img']:'',
            'cn_name'=>isset($param['cn_name'])?$param['cn_name']:'',
            'en_name'=>isset($param['en_name'])?$param['en_name']:'',
            'title'=>isset($param['title'])?$param['title']:'',
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'description'=>isset($param['description'])?$param['description']:'',
            'abstract'=>isset($param['abstract'])?$param['abstract']:'',
            'content'=>isset($param['content'])?$param['content']:'',
            'url'=>isset($param['url'])?$param['url']:'',
            'jump'=>isset($param['jump'])?$param['jump']:'default',
            'other'=>isset($param['other'])?$param['other']:'',
            'sort'=>isset($param['sort'])?$param['sort']:0,
            'pid'=>isset($param['pid'])?$param['pid']:0,
            "a_show"=>$param['a_show'],
        ]);
        return json($this->info);
    } 


}